<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="Js/web3.js"></script>
</head>
<br>
<input type ="button" value ="查看账号">  </br>
<input type ="button" value ="注册账号">  </br>
<input type ="button" value ="解锁第一个账号">  </br>
<input type ="button" value ="第一个账号转账给最后一个">  </br>
<input type ="button" value ="查看最后一个账号余额">  </br>
<script>

    var userArr=[]

    var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));

    document.querySelectorAll('input')[0].onclick=function()
    {
        web3.eth.getAccounts( function (err,ret){
            userArr = ret
            console.log(ret)
        })

    }
    document.querySelectorAll('input')[1].onclick=function()
    {
        if(userArr.length<1) {
            alert('请先查看账号')
            return
        }
        web3.personal.newAccount( "123456", function (err,ret){
            console.log(ret)
            userArr.push(ret)
        })

    }
    document.querySelectorAll('input')[2].onclick=function()
    {

        if(userArr.length<1) {
            alert('请先查看账号')
            return
        }
        var user = userArr[0]
        web3.personal.unlockAccount(user,'123456', function (err,ret){
            console.log('账号  :  '+user + ' 解锁 :  ' +( ret? '成功':'失败'))
        }.bind(this))

    }

    document.querySelectorAll('input')[3].onclick=function()
    {
        if(userArr.length<1) {
            alert('请先查看账号')
            return
        }
        var acc0 = userArr[0]
        var acc9 = userArr[userArr.length -1]
        web3.eth.sendTransaction({
            from: acc0,
            to: acc9,
            value: web3.toWei(0.01)
        },function(err,ret){
            console.log('交易码 : '+ret)
        })

    }


    document.querySelectorAll('input')[4].onclick=function()
    {
        if(userArr.length<1) {
            alert('请先查看账号')
            return
        }
        var user = userArr[userArr.length -1]
        web3.eth.getBalance(user, function (err,ret){
            console.log('账号  :  '+user + ' 余额 :  ' + ret)
        }.bind(this))

    }

</script>

</body>
</html>